<?php

/*******************************************************************
 Zenith Picture Gallery
 Written by and copyright (c) Ali Almossawi
 http://www.cyberiapc.com

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License, a copy of 
 which is made available to you with this package.
 This program is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A  PARTICULAR PURPOSE.
 
 File: marked_pics.php
 Description: Displays and allows download of marked pictures.
 Markings are bound to an IP address and are persisted until the
 session expires.  You may wish to edit line 36.
 Random quote: "The true measure of man is how he treats someone who
 can do him absolutely no good." -Samuel Johnson
*******************************************************************/

session_start();
session_cache_expire(360);
require_once('config.php');
require_once('functions/f_global.php');
require_once('functions/f_db.php');

//what's the state of the gallery?
isIpBlacklisted(getenv("REMOTE_ADDR"), $config, $lang);
if($config['gallery_off'] && !adminStatus($config))   doBox("error", $lang['gallery_off_msg'], $config, $lang);
elseif($config['private'] && !loggedIn($config))   doBox("error", $lang['private_msg'], $config, $lang);
elseif(!$config['marking_allowed'])   doBox("error", $lang['not_authorized_msg'], $config, $lang);

//if this page loads too slowly or a lot of strain is being placed on the server, set 
//generate_small_thumbs to 0 to use your thumbnails from the file system instead
$generate_small_thumbs = 1; 

//on form submit
if ($_POST['submit'] && isset($_POST['paths'])) {
	$set = array2set($_POST['paths'], "1", "value", "", "");
	
	//email admin
	/*if($config['mail_on_add']) {
		$body = "Hello,\n\nThis is to inform you that one or more marked pictures have been downloaded from your Zenith gallery.\n\n
		This email was automatically generated by the Zenith Picture Gallery script.";
		$headers = "From: {$config['title']} <{$config['admin_email']}>\r\n";
		mail($config['admin_email'],"{$config['title']} Pictures downloaded",$body,$headers);
	}*/

	$redirect = "Location: zipfile.php?m=1&paths=$set";
	header("$redirect");
	exit();
}

//are we adding or deleting a pic?
if (isset($_GET['pid'])) {
	$pid = $_GET['pid'];
	
	//if pid isn't numeric, all bets are off
	assertActivate();
	assert(is_numeric($pid));

	if($_GET['do'] == "mark") {
		//get user's existing marked pics and add this one to the collection
		$marked_pics = $_SESSION["marked_".getenv("REMOTE_ADDR")];
		$marked_pics[$pid] = $pid; //to avoid duplicates, we set the key to $pid
		$_SESSION["marked_".getenv("REMOTE_ADDR")] = $marked_pics;
		 
		//...and redirect
		$redirect = "Location: marked_pics.php?status=1m";
		header("$redirect");
		exit();
	}
	elseif($_GET['do'] == "unmark") {
		//get user's existing marked pics and remove this one from the collection
		$marked_pics = $_SESSION["marked_".getenv("REMOTE_ADDR")];
		$marked_pics[$pid] = "";
		unset($marked_pics[$pid]);
		$_SESSION["marked_".getenv("REMOTE_ADDR")] = $marked_pics;
		 
		//...and redirect
		$redirect = "Location: marked_pics.php?status=1u";
		header("$redirect");
		exit();
	}
}

$marked_pics = $_SESSION["marked_".getenv("REMOTE_ADDR")];
if(sizeof($marked_pics) == 0)   doBox("msg", $lang['no_marked_pics_msg'], $config, $lang);

require('head.php');

echo "<form name='frmDownloadMarked' action='{$_SERVER['PHP_SELF']}' method='post' style='margin:0;padding:0'>
<table class='table_layout_sections' style='width:{$config['gallery_width']}' cellpadding='2' cellspacing='0'>
<tr><td style='background-image:url(" . getSkinElement($config['stylesheet'], "images/td_back_mid.gif") . ")' class='cell_header' colspan='$td_width%'>{$lang['marked_pics']}</td></tr>
<tr><td class='search_page_cell'><span class='tiny_text_dark'>{$lang['marked_pics_intro']}</span></td></tr>
<tr><td>
";

//display all marked pics
foreach($marked_pics as $key => $value) {
	$statement = "SELECT file_name FROM {$config['table_prefix']}pictures WHERE pid=$key";
	$result = mysql_query($statement);
	$row = mysql_fetch_array($result);
	if($row['file_name'] == "")   continue; //if the pid was invalid, don't show anything
	
	//we need to get the full server path each time to pass to zipfile.php
	$at_full_server = getPicturesPath($config, $row['file_name'], 1);
	echo "<input type='hidden' name='paths[]' value='$at_full_server' />
	";
	
	//if we're using existing thumbnails, get this pic's thumbnail's path
	if(!$generate_small_thumbs) {
		$at_full = getPicturesPath($config, $row['file_name'], 0);
		$at_thumb = getThumbPath($at_full, $at_full_server, $config['thumb_suffix']);
	}

	//output to screen
	echo "<a href='marked_pics.php?do=unmark&amp;pid=$key'>
	";
	if($generate_small_thumbs) {
		echo "<img src='thumbify.php?pic={$row['file_name']}&w=100&h=50&folder=uploads&smart_resize=1' alt='{$row['file_name']}' title='{$row['file_name']}' class='thumb' border='0' style='margin-right:3px;margin-bottom:3px' />
		";
	}
	elseif(!$generate_small_thumbs) {
		echo "<img src='{$at_thumb[0]}' class='thumb' border='0' style='margin-right:3px;margin-bottom:3px' />
		";
	}
	echo "</a>
	";
}

echo "</td></tr>
<tr><td class='cell_foot'>

<div align='center'><input type='submit' name='submit' value='{$lang['download_all']}' class='submitButton3' /></div>
</td></tr>
</table>
</form>";



include('foot.php');

?>